#ifndef _FUNCTION_H_
#define _FUNCTION_H_
#include <tchar.h>
#include <stdio.h>
#define SafeCloseHandle(handle)	if((handle) != NULL){CloseHandle((handle));(handle) = NULL;}
#define SafeCloseWSAHandle(handle) if((handle) != NULL){WSACloseEvent((handle));(handle) = NULL;}
#define SafeCloseSocket(socket) if((socket) != INVALID_SOCKET){closesocket((socket));(socket) = INVALID_SOCKET;}
#define SafeCloseFile(pFile) if((pFile) != NULL){fclose((pFile));(pFile) = NULL;}
#define SafeDelete(p) if((p) != NULL){delete (p);(p) = NULL;}
#define SafeDeleteArray(p) if((p) != NULL){delete [](p);(p) = NULL;}

#define GET_IPSTRING(strIP,dwIP)   strIP.Format(_T("%d.%d.%d.%d"), (dwIP) & 0x0ff,((dwIP) >> 8) & 0x0ff,((dwIP) >> 16) & 0x0ff,((dwIP) >> 24) & 0x0ff)
#define GET_IPCHAR(szIP, dwIP)    sprintf(szIP, _T("%d.%d.%d.%d"), (dwIP) & 0x0ff,((dwIP) >> 8) & 0x0ff,((dwIP) >> 16) & 0x0ff,((dwIP) >> 24) & 0x0ff)
#define GET_IPSTRING_REVERSE(strIP,dwIP)   strIP.Format(_T("%d.%d.%d.%d"), ((dwIP) >> 24) & 0x0ff, ((dwIP) >> 16) & 0x0ff, ((dwIP) >> 8) & 0x0ff, (dwIP) & 0x0ff)
#define GET_IPCHAR_REVERSE(szIP, dwIP)    sprintf(szIP, _T("%d.%d.%d.%d"), ((dwIP) >> 24) & 0x0ff, ((dwIP) >> 16) & 0x0ff, ((dwIP) >> 8) & 0x0ff, (dwIP) & 0x0ff)

#ifdef _UNICODE
	#define  Safestrcpy wcscpy_s
	#define  Safestrncpy wcsncpy_s
	#define  Safesprintf swprintf_s
	#define  Safestrcat wcscat_s
	#define  Safestrlen wcslen
	#define  Safestrcmp wcscmp
	#define  Safestrstr wcsstr
	#define Safefopen _wfopen
#else
	#define  Safestrcpy strcpy_s
	#define  Safestrncpy strncpy_s
	#define  Safesprintf  sprintf_s
	#define  Safestrcat strcat_s
	#define  Safestrlen strlen
	#define  Safestrcmp strcmp
	#define  Safestrstr strstr
	#define  Safefopen fopen
#endif


static void CLOSE_THREAD(HANDLE& hThread, DWORD dwTimeOut = INFINITE)
{
	if(hThread)
	{
		DWORD dwRet = 0;
		DWORD nTimes = 0;
		dwRet = WaitForSingleObject(hThread, 0);
		while( dwRet != WAIT_OBJECT_0 && dwRet != WAIT_FAILED)
		{
			MSG msg;
			if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			dwRet = WaitForSingleObject(hThread, 10);
			if((++nTimes) * 10 >= dwTimeOut)
			{
				DWORD dwExitCode = 0;
				GetExitCodeThread(hThread, &dwExitCode);
				TerminateThread(hThread, dwExitCode);
				break;
			}
		}
		CloseHandle(hThread);
		hThread = NULL;
	}
}

static int Base64Enc(char *pszOutBuf, char* pszInBuf, int nInSize) 
{
	static char *base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	int buflen = 0;

	while (nInSize > 0) 
	{
		*pszOutBuf++ = base64_encoding[ (pszInBuf[0] >> 2 ) & 0x3f];
		if (nInSize > 2) 
		{
			*pszOutBuf++ = base64_encoding[((pszInBuf[0] & 3) << 4) | (pszInBuf[1] >> 4)];
			*pszOutBuf++ = base64_encoding[((pszInBuf[1] & 0xF) << 2) | (pszInBuf[2] >> 6)];
			*pszOutBuf++ = base64_encoding[pszInBuf[2] & 0x3F];
		} 
		else 
		{
			switch (nInSize) 
			{
			case 1:
				*pszOutBuf++ = base64_encoding[(pszInBuf[0] & 3) << 4 ];
				*pszOutBuf++ = '=';
				*pszOutBuf++ = '=';
				break;
			case 2:
				*pszOutBuf++ = base64_encoding[((pszInBuf[0] & 3) << 4) | (pszInBuf[1] >> 4)];
				*pszOutBuf++ = base64_encoding[((pszInBuf[1] & 0x0F) << 2) | (pszInBuf[2] >> 6)];
				*pszOutBuf++ = '=';
				break;
			}
		}
		pszInBuf += 3;
		nInSize -= 3;
		buflen += 4;
	}
	*pszOutBuf = 0;
	return buflen;
}

static void Base64Decode(char *pszInBuf, int nInSize, char *pszOutbuf) 
{
	int i = 0, j = 0;
	int base64_decode_map_int[256] = 
	{
		255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
		255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
		255, 255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
		255, 0, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
		15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 26, 27, 28,
		29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
		49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
		255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
		255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
		255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
		255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
		255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
		255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
	};
	char base64_decode_map[256];
	for (i = 0; i < 256; i++) 
	{ 
		base64_decode_map[i] = base64_decode_map_int[i];
	}
	i = 0;

	for (; i < nInSize; i += 4) 
	{
		pszOutbuf[j++] = base64_decode_map[pszInBuf[i]] << 2 |
			base64_decode_map[pszInBuf[i + 1]] >> 4;

		pszOutbuf[j++] = base64_decode_map[pszInBuf[i + 1]] << 4 |
			base64_decode_map[pszInBuf[i + 2]] >> 2;

		pszOutbuf[j++] = base64_decode_map[pszInBuf[i + 2]] << 6 |
			base64_decode_map[pszInBuf[i + 3]];
	}
	pszOutbuf[j] = '\0';
}

inline BOOL CreateDir(TCHAR *pszPath)
{
	if(pszPath == NULL || Safestrlen(pszPath) == 0)
	{
		return FALSE;
	}
	TCHAR *pTemp = Safestrstr(pszPath, _T("\\"));
	if(pTemp == NULL)
	{
		return FALSE;
	} 
	pTemp++;
	TCHAR szPath[MAX_PATH] = {0};
	TCHAR *pTemp1 = Safestrstr(pTemp, _T("\\"));
	while(pTemp1 != NULL)
	{
		Safestrncpy(szPath, sizeof(szPath)/sizeof(TCHAR), pszPath, pTemp1 - pszPath);
		if(!CreateDirectory(szPath, NULL))
		{
			if(GetLastError() != ERROR_ALREADY_EXISTS)
			{
				return FALSE;
			}
		}
		pTemp1++;
		pTemp1 = Safestrstr(pTemp1, _T("\\"));
	}
	return TRUE;
}

static BOOL MultiByte2WideChar(char *pszInBuf, wchar_t *pszOutBuf)
{
	if(pszInBuf == NULL || pszOutBuf == NULL)
	{
		return FALSE;
	}
	
	DWORD dwNum = MultiByteToWideChar(CP_ACP, 0, pszInBuf, -1, NULL, 0);
	MultiByteToWideChar (CP_ACP, 0, pszInBuf, -1, pszOutBuf, dwNum);
	return TRUE;
}

static BOOL WideChar2MultiByte(wchar_t *pszInBuf, char *pszOutBuf)
{
	if(pszInBuf == NULL || pszOutBuf == NULL)
	{
		return FALSE;
	}
	DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pszInBuf,-1,NULL,0,NULL,FALSE);
	WideCharToMultiByte (CP_OEMCP,NULL,pszInBuf,-1,pszOutBuf,dwNum,NULL,FALSE);
	return TRUE;
}

static BOOL IsEnglishVersion()
{
	LANGID lID = GetSystemDefaultLangID();
	return lID == 0x0409;
}
#endif

